C.DefaultCredits = 20;

LIQ_TABLE = {
	0.004,
	0.004,
	0.002
}

function C.InitCredits( ply )
	
	local cur = ply:GetPData( "Cosmos_Credits" );
	
	if( !cur ) then
		
		C.SetCredits( ply, C.DefaultCredits );
		
	end
	
end

function C.RefreshCredits( ply, amt )
	
	local cur = amt or C.Credits( ply );
	
	umsg.Start( "RC", ply );
		umsg.Long( cur );
	umsg.End();
	
end

function C.SetCredits( ply, amt )
	
	ply:SetPData( "Cosmos_Credits", amt );
	C.RefreshCredits( ply, amt );
	
end

function C.AddCredits( ply, amt )
	
	local cur = C.Credits( ply );
	local n = cur + amt;
	C.SetCredits( ply, n );
	
end

function C.RemoveCredits( ply, amt )
	
	local cur = C.Credits( ply );
	local n = math.max( cur - amt, 0 );
	C.SetCredits( ply, n );
	
end

function C.HasCredits( ply, amt )
	
	local cur = C.Credits( ply );
	
	if( cur >= amt ) then
		
		return true;
		
	end
	
	return false;
	
end

function ccResetCredits( ply, cmd, args )
	
	if( C.Credits( ply ) < C.DefaultCredits ) then
		
		C.SetCredits( ply, C.DefaultCredits );
		
	end
	
end
concommand.Add( "c_resetcredits", ccResetCredits );

function C.RefundEnt( e )
	
	local own = e.Owner;
	
	if( own and own:IsValid() ) then
		
		C.AddCredits( own, math.Round( e.CosmicPrice * 0.75 ) );
		
	end
	
end

function GM:ShowSpare2( ply )
	
	local trace = { };
	trace.start = ply:GetShootPos();
	trace.endpos = trace.start + ply:GetAimVector() * 120;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() and tr.Entity.Cosmic and tr.Entity.CosmicType == CE_CONTAINER ) then
		
		if( tr.Entity.Owner and tr.Entity.Owner:IsValid() and tr.Entity.Owner == ply ) then
			
			umsg.Start( "LR", ply );
				umsg.Short( #tr.Entity.Resources );
				
				for _, v in pairs( tr.Entity.Resources ) do
					
					umsg.Long( v );
					
				end
				
			umsg.End();
			
		else
			
			C.PCM( ply, Vector( 200, 0, 0 ), "This isn't yours you thief!" );
			
		end
		
	end
	
end

function ccLiquifyResource( ply, cmd, args )
	
	local res = tonumber( args[1] );
	
	local trace = { };
	trace.start = ply:GetShootPos();
	trace.endpos = trace.start + ply:GetAimVector() * 120;
	trace.filter = ply;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() and tr.Entity.Cosmic and tr.Entity.CosmicType == CE_CONTAINER ) then
		
		if( tr.Entity.Owner and tr.Entity.Owner:IsValid() and tr.Entity.Owner == ply ) then
			
			if( res == 0 ) then
				
				for _, v in pairs( tr.Entity.Resources ) do
					
					local amt = tr.Entity:GetAmount( v );
					local cash = math.Round( amt * LIQ_TABLE[v] );
					tr.Entity:SetAmount( v, 0 );
					C.AddCredits( ply, cash );
					
				end
				
			else
				
				local amt = tr.Entity:GetAmount( res );
				local cash = math.Round( amt * LIQ_TABLE[res] );
				tr.Entity:SetAmount( res, 0 );
				C.AddCredits( ply, cash );
				
			end
			
		else
			
			ply:Kick( "Tried to use an engine command!" ); -- Deterrant from manually entering values into console
			
		end
		
	end
	
end
concommand.Add( "c_e_lr", ccLiquifyResource );

function ccaGiveMoney( ply, cmd, args )
	
	if( !ply:IsAdmin() ) then return end
	
	if( #args < 2 ) then
		
		return;
		
	end
	
	local targ = C.FindPlayer( args[1] );
	local amt = tonumber( args[2] );
	
	C.AddCredits( targ, amt );
	
	C.PCM( ply, Vector( 0, 200, 0 ), "Gave " .. targ:Nick() .. " " .. tostring( amt ) .. " credits." );
	C.PCM( ply, Vector( 0, 200, 0 ), ply:Nick() .. " gave you " .. tostring( amt ) .. " credits." );
	
end
concommand.Add( "c_a_givemoney", ccaGiveMoney );